package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.TripMini;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class RecommendTripAdapter extends BaseTripMiniAdapter implements ObservableAdapter{

    private LayoutInflater mInflater;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();
    
    public RecommendTripAdapter(Context context, RemoteResourceManager rrm) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mRrm = rrm;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mRrm.addObserver(mResourcesObserver);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.recommend_trip_item, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.ivTripImage);
            holder.name = (TextView) convertView.findViewById(R.id.tvTripName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
       TripMini trip = (TripMini) getItem(position);
       if (trip.getPhoto() != null) {
            final Uri photoUri = Uri.parse(trip.getPhoto());
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                holder.photo.setImageBitmap(bitmap);
            } catch (IOException e) {
                holder.photo.setImageResource(R.drawable.trip_default_photo);
            }
        }   else {
            holder.photo.setImageResource(R.drawable.trip_default_photo);
        }
        convertView.setPadding(15, 5, 15, 5);
        holder.name.setText(trip.getName());
        return convertView;
    }
    
    @Override
    public void setGroup(Group<TripMini> g) {
        super.setGroup(g);

        for (TripMini it : g) {
            if (it.getPhoto() != null)  {
                Uri photoUri = Uri.parse(it.getPhoto());
                if (!mRrm.exists(photoUri)) {
                    mRrm.request(photoUri);
                }
            }
        }
    }
    
    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhotos);
        }
    }

    private Runnable mUpdatePhotos = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };
    
    @Override
    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhotos);
        mRrm.deleteObserver(mResourcesObserver);
    }
    
    private static class ViewHolder {
        ImageView photo;
        TextView name;
    }
}
